package com.roomio.mail.service.impl;

import java.util.Map;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.velocity.VelocityEngineUtils;
import com.roomio.mail.service.MailService;

@Service("mailService")
@Transactional
public class MailServiceImpl implements MailService {
	protected final Log log = LogFactory.getLog(getClass());
	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private VelocityEngine velocityEngine;

	

	private MimeMessageHelper prepareMessage(final String subject, final String fromAddress, final String[] toAddresses, final String[] ccAddresses,
			final String[] bccAddresses, MimeMessage mimeMessage) throws MessagingException {
		MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
		message.setSubject(subject);
		message.setFrom(new InternetAddress(fromAddress));
		for (String toAddress : toAddresses) {
			log.debug("Adding recipient: " + toAddress);
			message.addTo(new InternetAddress(toAddress));
		}
		if (ccAddresses != null) {
			for (String ccAddress : ccAddresses) {
				log.debug("Adding recipient: " + ccAddress);
				message.addCc(ccAddress);
			}
		}
		if (bccAddresses != null) {
			for (String bccAddress : bccAddresses){
				log.debug("Adding recipient: " + bccAddress);
				message.addBcc(bccAddress);
			}
		}

		return message;
	}

	private String wrapHTMLTags(String content) {
		String root = "ogtemplate";
		if (content != null)
			content.trim();
		int startIndex = content.indexOf("<" + root + ">");
		int endIndex = content.indexOf("</" + root + ">");
		return content.substring(startIndex + root.length() + 2,// to account
																// for < and >
				endIndex);
	}

	
	/**
	 * Tokenizes Emails separated with a comma ","
	 * @param subject
	 * @param fromAddress
	 * @param toAddresses
	 * @param ccAddresses
	 * @param bccAddresses
	 * @param templateName
	 * @param mergeProperties
	 */
	public void sendTemplateMail(final String subject, final String fromAddress, final String toAddresses, final String ccAddresses,
			final String bccAddresses, final String templateName, final Map<Object, Object> mergeProperties) {
		String[] to = null; 
		String[] cc = null;
		String[] bcc = null;
		if(toAddresses!=null)
			to = toAddresses.split(",");
		if(ccAddresses!=null)
			cc = ccAddresses.split(",");
		if(bccAddresses!=null)
			bcc = bccAddresses.split(",");
		sendTemplateMail(subject, fromAddress, to, cc, bcc, templateName, mergeProperties);
	}
	
	/**
	 * Sends a mail based on velocity template
	 */
	public void sendTemplateMail(final String subject, final String fromAddress, final String[] toAddresses, final String[] ccAddresses,
			final String[] bccAddresses, final String templateName, final Map<Object, Object> mergeProperties) {
		// Generate the content using a template
		final String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, templateName, mergeProperties);
		mailSender.send(new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws MessagingException {
				MimeMessageHelper message = prepareMessage(subject, fromAddress, toAddresses, ccAddresses, bccAddresses, mimeMessage);
				// send HTML mail
				message.setText(content, true);
			}
		});
	}

	/**
	 * Sends a simple text based mail
	 */
	public void sendMail(final String subject, final String fromAddress, final String[] toAddresses, final String[] ccAddresses,
			final String[] bccAddresses, final String content, final boolean isHTML) {
		mailSender.send(new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws MessagingException {
				MimeMessageHelper message = prepareMessage(subject, fromAddress, toAddresses, ccAddresses, bccAddresses, mimeMessage);
				message.setText(content, isHTML);
			}
		});
	}

}
